package com.stlm2.dems.service;

import com.stlm2.dems.dao.DdmGoodPropertyDao;
import com.stlm2.dems.dao.DdmGoodsDao;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.DdmGoodProperty;
import com.stlm2.dems.entity.DdmGoods;
import com.stlm2.dems.entity.User;
import com.stlm2.util.Memcached;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.DdmGoodPropertyRelation;
import com.stlm2.dems.dao.DdmGoodPropertyRelationDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service("ddmGoodPropertyRelationService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class DdmGoodPropertyRelationService extends BaseServiceImpl<DdmGoodPropertyRelation> {

	@Resource
	private DdmGoodPropertyRelationDao ddmGoodPropertyRelationDao;
	@Resource
	private DdmGoodPropertyDao ddmGoodPropertyDao;

	@Override
	protected BaseDao <DdmGoodPropertyRelation> getDao() {
		return ddmGoodPropertyRelationDao;
	}
	@Resource
	private Memcached memcached;
	@Resource
	private UserDao userDao;
	@Resource
	private DdmGoodsDao ddmGoodsDao;

	/*
	*zlq
	* 商品添加属性
	* token：
	* goodsId:商品ID
	* propertyIds：属性ID
	*
	 */
	public String add(ModelMap model, String token, String goodsId, String propertyIds){
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if (!NumberUtils.isDigits(goodsId)) {
			return CodeContent.getModelByCode(0, "商品ID不能为空", model);
		}
		if (StringUtils.isEmpty(propertyIds)) {
			return CodeContent.getModelByCode(0, "增值服务ID不能为空", model);
		}
		DdmGoods ddmGoods=ddmGoodsDao.getById(Integer.parseInt(goodsId));
		if(ddmGoods==null){
			return CodeContent.getModelByCode(0, "商品ID不存在", model);
		}
		//防止重复提交
		Object o = memcached.get(ddmGoods.getId()+"addDdmGoodPropertyRelation");
		if(o!=null){
			return CodeContent.getModelByCode(0, "操作频繁", model);
		}else {
			memcached.set(ddmGoods.getId()+"addDdmGoodPropertyRelation",1,"1");
		}
		List<DdmGoodPropertyRelation> ddmGoodPropertyRelationList =new ArrayList<DdmGoodPropertyRelation>();
		String [] propertyId=propertyIds.split(",");
		for(String id:propertyId){
			DdmGoodProperty ddmGoodProperty=ddmGoodPropertyDao.getById(Integer.parseInt(id));
			if(ddmGoodProperty==null){
				return CodeContent.getModelByCode(0, "属性ID不存在", model);
			}
			DdmGoodPropertyRelation ddmGoodPropertyRelation1 =new DdmGoodPropertyRelation();
			ddmGoodPropertyRelation1.setGoodsId(ddmGoods.getId());
			ddmGoodPropertyRelation1.setPropertyId(ddmGoodProperty.getId());
			List <DdmGoodPropertyRelation> ddmGoodPropertyRelationList1=ddmGoodPropertyRelationDao.queryPageView(-1,-1,ddmGoodPropertyRelation1).getRecords();
			if(ddmGoodPropertyRelationList1.size()==0){
				ddmGoodPropertyRelationList.add(ddmGoodPropertyRelation1);
				//return CodeContent.getModelByCode(0, "商品已经添加过:" + ddmGoodProperty.getName(), model);
			}

		}
		for(DdmGoodPropertyRelation ddmGoodPropertyRelation:ddmGoodPropertyRelationList){
			ddmGoodPropertyRelationDao.add(ddmGoodPropertyRelation);
		}
		return CodeContent.getModelByCode(1, "", model);
	}


	/*
*删除商品属性
* zlq
* token:
* id: 记录ID
 */
	public String delete(ModelMap model, String token,String id){
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if (!NumberUtils.isDigits(id)) {
			return CodeContent.getModelByCode(0, "ID不能为空", model);
		}
		DdmGoodPropertyRelation ddmGoodPropertyRelation=ddmGoodPropertyRelationDao.getById(Integer.parseInt(id));
		if(ddmGoodPropertyRelation==null){
			return CodeContent.getModelByCode(0, "ID不存在", model);
		}
		ddmGoodPropertyRelationDao.delete(ddmGoodPropertyRelation.getId());
		return CodeContent.getModelByCode(1, "", model);
	}

	/*
*zlq
* 商品查找叮当猫增值服务列表
* token:
* goodsId:商品ID
*/
	public String getlist(ModelMap model, String token,String goodsId) {
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if (StringUtils.isEmpty(goodsId)) {
			return CodeContent.getModelByCode(0, "商品ID不能为空", model);
		}
		DdmGoods ddmGoods=ddmGoodsDao.getById(Integer.parseInt(goodsId));
		if(ddmGoods!=null){
			List<DdmGoodPropertyRelation> ddmGoodPropertyRelationList=ddmGoodPropertyRelationDao.getByGoodsId(ddmGoods.getId());
			return CodeContent.getModelByCode(1, "", convertList(ddmGoodPropertyRelationList));
		}

		return CodeContent.getModelByCode(1, "", model);
	}

	public List<ModelMap> convertList(List<DdmGoodPropertyRelation> ddmGoodPropertyRelationList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (ddmGoodPropertyRelationList != null && ddmGoodPropertyRelationList.size() > 0) {
			for (DdmGoodPropertyRelation ddmGoodPropertyRelation : ddmGoodPropertyRelationList) {
				ModelMap map = buildModelList(ddmGoodPropertyRelation);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModelList(DdmGoodPropertyRelation ddmGoodPropertyRelation ) {
		ModelMap model = new ModelMap();
		model.put("id",ddmGoodPropertyRelation.getId());
		model.put("name",ddmGoodPropertyRelation.getDdmGoodPropertyList().size()!=0?ddmGoodPropertyRelation.getDdmGoodPropertyList().get(0).getName():"");

		return  model;
	}
}